/*
 If the strain's parent has a state of Initial Isolate, then
 that strain is the progenitor
 */
function getUpstreamStrains(strainArray, strainObj, callback) {
    var strain = strainObj.strainParents;
    if (strain == null || strain.length == 0 || strain.indexOf("YO") == -1) {
        //Ext.Msg.alert("Info", "Strain " + strainObj.name + " is missing parent relationship or has invalid state:" + strainObj.status);
        callback();
    }
    if (strain != null && strain.length > 1 && strain.indexOf("YO") != -1) {
        //var uri = "/addama-rest/dudley-sample/path/datasource/YO/" + strain + "/annotations";
        var uri = "/solr/select/?q=yoKey:" + strain + "&wt=json&rows=1&fl=yoKey,yoParent,yoStatus,yoAlias,yoSiblings";
        Ext.Ajax.request({
            url: uri,
            method: 'GET',
            success: function(response) {
                var resp = Ext.util.JSON.decode(response.responseText);
                var annoObj = resp.response.docs[0];
                //need this check otherwise program will go into an infinite loop as it traverses up
                if (strain == annoObj.yoParent) {
                    Ext.Msg.alert("Info", "Please fix parent relationship: Strain can not be parent of itself - " + strain);
                    //callback;
                }
                var parentObj = new Object();
                parentObj.key = annoObj.yoKey;
                parentObj.parents = annoObj.yoParent;
                parentObj.status = annoObj.yoStatus;
                if (annoObj.yoStatus != null && (annoObj.yoStatus == "Undetermined" || annoObj.yoStatus.indexOf("Not") != -1)) {
                    displayNonStatus = true;
                }
                parentObj.alias = annoObj.yoAlias;
                strainArray.push(parentObj);
                var siblings = annoObj.yoSiblings;
                if (siblings == null) {
                    siblings = "";
                }
                var siblingArray = siblings.split("_");
                lookupSiblingStrains(strainArray, siblingArray, function() {
                    if (status != "Initial Isolate" && strain != annoObj.yoParent) {
                        getUpstreamStrains(strainArray, annoObj, callback);
                    } else {
                        callback();
                    }
                });
            },
            failure: function() {
                Ext.Msg.alert("Error", "Strain " + strainObj.name + " recorded parent " + strainObj.strainParents + " is invalid");
                callback();
            }
        });
    }
}

function getDownstreamStrains(downStreamStrainArray, strainList, callback) {
    if (strainList == null || strainList.length < 1) {
        callback();
    }
    var strain = strainList.pop();
    if (strain == null || strain.length < 1) {
        callback();
    }
    //var uri = "/addama-rest/dudley-sample/path/datasource/YO/search?MATCHING_ALL_TERMS=true
    // &PROJECTION=alias&PROJECTION=strainName&PROJECTION=status&strainParents=" + strain;
    var uri = "/solr/select/?q=yoParent:" + strain + "&wt=json&rows=1&fl=yoKey,yoParent,yoStatus,yoAlias,yoSiblings";
    if (strain != null && strain.length > 1) {
        Ext.Ajax.request({
            url: uri,
            method: 'GET',
            success: function(response) {
                var resultObj = Ext.util.JSON.decode(response.responseText);
                var resp = Ext.util.JSON.decode(response.responseText);
                var resultArray = resp.response.docs;
                for (var r = 0; r < resultArray.length; r++) {
                    var childObj = new Object();
                    var annoObj = resultArray[r];
                    childObj.key = annoObj.yoKey;
                    childObj.parents = strain;
                    childObj.status = annoObj.yoStatus;
                    if (childObj.status == "Undetermined" || childObj.status.indexOf("Not") != -1) {
                        displayNonStatus = true;
                    }
                    childObj.alias = annoObj.yoAlias;
                    downStreamStrainArray.push(childObj);
                    strainList.push(childObj.key);
                }
                if (strainList == null || strainList.length < 1) {
                    callback();
                } else {
                    getDownstreamStrains(downStreamStrainArray, strainList, callback);
                }
            },
            failure: function() {
                Ext.Msg.alert("Info", "No progeny records found for parent strain uri:" + uri);
                callback();
            }
        });
    }
}

/*
 If the strain's parent has a state of Initial Isolate, then
 that strain is the progenitor
 */
function lookupSiblingStrains(strainArray, siblingList, callback) {
    if (siblingList == null || siblingList.length == 0) {
        callback();
    }
    var siblingJson = new Object();
    siblingJson["key"] = siblingList.pop();
    var myArray = new Array();
    myArray[0] = siblingJson["key"];
    if (siblingJson["key"] != null && siblingJson["key"].length > 1) {
        //var uri = "/addama-rest/dudley-sample/path/datasource/YO/" + siblingJson["key"] + "/annotations";
        /*
        "/solr/select/?q=" + searchTerm + "&wt=json&rows=" + maxresults + "&fl=yoKey," +
           "yoStatus,yoAlias,yoMatingType,yoMatingTypeMethod,yoIsSC,yoIsSCMethod,yoPloidy,"
         */
        var uri = "/solr/select/?q=yoKey:" + siblingJson["key"] + "&wt=json&rows=1&fl=yoKey,yoParent,yoStatus,yoAlias";
        Ext.Ajax.request({
            url: uri,
            method: 'GET',
            success: function(response) {
                var resp = Ext.util.JSON.decode(response.responseText);
                var annoObj = resp.response.docs[0];                
                siblingJson["parents"] = annoObj.yoParent;
                siblingJson["status"] = annoObj.yoStatus;
                if (annoObj.yoStatus != null && (annoObj.yoStatus == "Undetermined" || annoObj.yoStatus.indexOf("Not") != -1)) {
                    displayNonStatus = true;
                }
                siblingJson["alias"] = annoObj.yoAlias;
                strainArray.push(siblingJson);
                if (siblingList == null || siblingList.length == 0) {
                    getDownstreamStrains(strainArray, myArray, function () {
                        callback();
                    });
                } else {
                    //find progenies of siblings
                    getDownstreamStrains(strainArray, myArray, function () {
                        lookupSiblingStrains(strainArray, siblingList, callback);
                    });
                }
            },
            failure: function() {
                Ext.Msg.alert("Error", "Failed retrieving annotations for sibling:" + siblingJson["key"]);
                callback();
            }
        });
    } else {
        callback();
    }
}

function selectStrain(yoKey) {
    //var yoUri = "/addama-rest/dudley-sample/path/datasource/YO/" + yoKey + "/meta";
    Dudley.lims.YOStrain.loadStrain(yoKey);
}